package net.qhzw.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.qhzw.admin.entity.PersonnelReward;
import net.qhzw.admin.mapper.PersonnelRewardMapper;
import net.qhzw.admin.param.PersonnelRewardParam;
import net.qhzw.admin.pojo.PersonnelRewardVo;
import net.qhzw.admin.pojo.PersonnelRewardVo2;
import net.qhzw.admin.query.PersonnelRewardQuery;
import net.qhzw.admin.service.PersonnelRewardService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PersonnelRewardServiceImpl implements PersonnelRewardService {


    @Autowired
    private PersonnelRewardMapper personnelRewardMapper;

    @Value("${config.imgage.view}")
    private String imageView;

    /****
     * 获取当前人员奖惩信息
     * @param id
     * @return
     */
    @Override
    public JsonResult getPersonnelReward(String id) {
        PersonnelRewardVo2 personnelRewardVo2 = personnelRewardMapper.selectPersonnelReward(id);
        return JsonResult.success(personnelRewardVo2);
    }


    /***
     * 获取奖惩信息列表
     * @param query
     * @return
     */
    @Override
    public JsonResult getPersonnelRewardList(PersonnelRewardQuery query) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        if (query.getRewardTime() != null && query.getRewardTime().length > 0) {
            query.setStartTime(query.getRewardTime()[0]);
            query.setEndTime(query.getRewardTime()[1]);
        }
        List<PersonnelRewardVo> personnelRewardVoList = personnelRewardMapper.selectList(query);
        for (PersonnelRewardVo personnelRewardVo : personnelRewardVoList) {
            if (personnelRewardVo.getPhoto() != null && !StringUtils.isEmpty(personnelRewardVo.getPhoto())) {
                personnelRewardVo.setPhoto(imageView + personnelRewardVo.getPhoto());
            }
        }
        PageInfo<PersonnelRewardVo> pageInfo = new PageInfo<>(personnelRewardVoList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());

        return JsonResult.success(resultMap);
    }


    /***
     * 更新人员奖惩信息
     * @param param
     * @return
     */
    @Override
    public JsonResult updatePersonnelReward(PersonnelReward param) {
        if (param.getId() == null || StringUtils.isEmpty(param.getId())) {
            //新增
            param.setRewardTime(new Date());
            param.setCreateTime(new Date());
            param.setId(UUIDUtil.getUUid());
            int row = personnelRewardMapper.insertSelective(param);
            return JsonResult.result(row, "新增失败");
        }

        int row = personnelRewardMapper.updateByPrimaryKeySelective(param);

        return JsonResult.result(row, "更新失败");
    }

    @Override
    public JsonResult deletePersonnelReward(String id) {
        int row = personnelRewardMapper.deleteByPrimaryKey(id);
        return JsonResult.result(row, "删除失败");
    }
}
